package cn.lingyangwl.framework.core;

import cn.lingyangwl.framework.core.config.LyThreadPoolConfig;
import cn.lingyangwl.framework.core.config.properties.LyHttpClientProperties;
import cn.lingyangwl.framework.core.config.properties.LyThreadPoolProperties;
import cn.lingyangwl.framework.core.response.EmptyRequestHandlerImpl;
import cn.lingyangwl.framework.core.response.ResponseFormatConfig;
import cn.lingyangwl.framework.core.response.format.ResponseFormatHandler;
import cn.lingyangwl.framework.core.response.wrap.WebInitializingAdvice;
import cn.lingyangwl.framework.core.utils.AsyncTaskExecutorWithTx;
import cn.lingyangwl.framework.core.utils.spring.SpringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnNotWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import org.springframework.web.context.request.RequestContextListener;

import javax.annotation.PostConstruct;

/**
 * @author shenguangyang
 */
@Import({
        IniEnvRegistrar.class,
        SpringUtils.class,
        LyThreadPoolConfig.class,
        AsyncTaskExecutorWithTx.class, ResponseFormatConfig.class,
        // 响应相关的
        WebInitializingAdvice.class, ResponseFormatHandler.class
})
@AutoConfiguration
@EnableConfigurationProperties({
        LyThreadPoolProperties.class, LyHttpClientProperties.class
})
public class LyCoreAutoConfiguration {
    private static final Logger log = LoggerFactory.getLogger(LyCoreAutoConfiguration.class);

    @Bean
    public RequestContextListener requestContextListener() {
        return new RequestContextListener();
    }

    @PostConstruct
    public void init() {
        log.info("init {}", this.getClass().getName());
    }

    @Bean
    @ConditionalOnNotWebApplication
    public EmptyRequestHandlerImpl emptyRequestHandler() {
        return new EmptyRequestHandlerImpl();
    }
}